<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends WxHomeController {

	function __construct() {
		parent::__construct();
		$this -> sidebar = "Index";
		$this -> load ->model("Admin_model");
        $this->load->model("Announcement_model");
        $this->load->model('Student_feedback_model');
        $this->load->model('Student_feedback_record_model');
        $this->load->model('Supervisor_feedback_model');
        $this->load->model('Supervisor_feedback_record_model'); 
	}

	function Index($login_role=0) {
		// $data['login_role']=$login_role;
		$result = $this ->Admin_model->get_single(array("id"=>$_SESSION[SESS_USER_HOME]['id']));
		// $data['sub_role']=explode(",",$result['sub_role_id']);
		$data['detail']=$result;
        unset($_SESSION[SESS_ASSESS]);
		$this->load->view('home/index',$data);
	}

    //信息反馈
    function Back(){
        $this->load->view('home/back');
    }
    //课堂教学评价
    function evaluation(){        
		$role=explode(',',$_SESSION[SESS_USER_HOME]['sub_role_id']);
		array_push($role, $_SESSION[SESS_USER_HOME]['role_id']);	
		$data['role']=array_merge(array_filter($role));	

        $result = $this ->Admin_model->get_single(array("id"=>$_SESSION[SESS_USER_HOME]['id']));
        $data['attend_id']= $result['attend_id']?$result['attend_id']:'0';
        unset($_SESSION[SESS_ASSESS]);
        $this->load->view('home/evaluation',$data);
    }

    //选择课程类型
    function choiceClassType($attend_id,$soure){
        $this->load->model("Select_model");
        $this->load->model("Attend_model");
        $result = $this->Select_model->get_list(array('type' => SelectType::CourseType[XPHEnum::Value], 'status' => CommonStatus::UnDeleted[XPHEnum::Value]));
        $data['class_type'] = array();
        foreach ($result as $k => $v) {
            array_push($data['class_type'], trim($v['option']));
        }
        unset($_SESSION[SESS_ASSESS]['cc_type']);
        $data['attend_id']=$attend_id;
        $data['soure']=$soure;
        if($attend_id){
            $attends  = $this ->Attend_model->get_single(array("id"=>$attend_id));
            $data['attends']=$attends;
        }
        $_SESSION[SESS_ASSESS]['soure'] = $soure;
        $this->load->view('home/choice_classtype',$data);
    }


    //其他材料提交
    function others(){
        $this->load->view('home/others');
    }


	function Broadcast(){
        // $title=$this->Announcement_model->query("select *  from db_announcement ");
        $sub_role=explode(",", $_SESSION[SESS_USER_HOME]['sub_role_id']);
        array_push($sub_role,$_SESSION[SESS_USER_HOME]['role_id']);
        $connar['where']=$this -> sqlEscape('status=?? and start<=?? and end >=??',array(CommonStatus::UnDeleted[XPHEnum::Value],time(),time()));
        foreach ($sub_role as $k => $v) {
        	if($k==0){
        		$connar['where'].=$this->sqlEscape(" and (find_in_set(??,role_id_str)!=0 ",array($v));
        	}else{
        		$connar['where'].=$this->sqlEscape(" or find_in_set(??,role_id_str)!=0 ",array($v));
        	}        	
        }
        $connar['where'].=")";
        $title=$this -> Announcement_model->get_list_full($connar);
        exit(retJson('获取标题', true,$title));        
    }   

    // 判断是否有待处理反馈
    function checkFeedback(){
        $role=explode(',',$_SESSION[SESS_USER_HOME]['sub_role_id']);
        array_push($role,$_SESSION[SESS_USER_HOME]['role_id']);
        $data= array();
        // 校领导和管理员角色时，不应该存在有管理员的反馈审核
        if(in_array(WebRole::SchoolLeader[XPHEnum::Value],$role) && in_array(WebRole::Manage[XPHEnum::Value],$role)){
            exit(retJson('反馈',true,$data));
        }
       if(in_array(WebRole::Manage[XPHEnum::Value],$role)){
            // $data['feedbackcount']=$this->Supervisor_feedback_model->query('select count(*)as count from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback where role_belong='.WebRole::Manage[XPHEnum::Value].' and status='.FeedBackStatus::ToAudit[XPHEnum::Value].' and (belong=0 or belong='.$_SESSION[SESS_USER_HOME]['id'].') and del_flag='.CommonStatus::UnDeleted[XPHEnum::Value])[0]['count'];
            $data['feedbackcount']=$this->Supervisor_feedback_model->query('select count(*)as count from(select * from db_student_feedback where status ='.FeedBackStatus::ToAudit[XPHEnum::Value].' and del_flag='.CommonStatus::UnDeleted[XPHEnum::Value].' and role_belong = '.WebRole::Manage[XPHEnum::Value].' union all select * from db_supervisor_feedback where status ='.FeedBackStatus::ToAudit[XPHEnum::Value].' and del_flag='.CommonStatus::UnDeleted[XPHEnum::Value].' and role_belong = '.WebRole::Manage[XPHEnum::Value].') feedback ')[0]['count'];
        //    echo $this->db->last_query();
            // $data['feedbackcountTeacher']=$this->Supervisor_feedback_model->query('select count(*)as count from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback where role_belong='.WebRole::Manage[XPHEnum::Value].' and status='.FeedBackStatus::ToAudit[XPHEnum::Value].' and belong='.$_SESSION[SESS_USER_HOME]['id'].' and del_flag='.CommonStatus::UnDeleted[XPHEnum::Value])[0]['count'];
       
        }else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value],$role)){
            $data['feedbackcount']=$this->Supervisor_feedback_model->query('select count(*)as count from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback LEFT JOIN db_admin a1 on a1.id=feedback.creator where feedback.status='.FeedBackStatus::ToAudit[XPHEnum::Value].' and a1.dept_name="'.$_SESSION[SESS_USER_HOME]['dept_name'].'" and role_belong='.WebRole::UnitSupervisorGroupLeader[XPHEnum::Value].' and del_flag='.CommonStatus::UnDeleted[XPHEnum::Value])[0]['count'];
            // echo $this->db->last_query();
            // $data['feedbackcountTeacher']=$this->Supervisor_feedback_model->query('select count(*)as count from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback LEFT JOIN db_admin a1 on a1.id=feedback.creator where feedback.status='.FeedBackStatus::ToAudit[XPHEnum::Value].' and a1.dept_name="'.$_SESSION[SESS_USER_HOME]['dept_name'].'" and role_belong='.WebRole::UnitSupervisorGroupLeader[XPHEnum::Value].' and del_flag='.CommonStatus::UnDeleted[XPHEnum::Value].' and belong='.$_SESSION[SESS_USER_HOME]['id'])[0]['count'];
        }
        // var_dump($data['feedbackcount']);
        // var_dump($data['feedbackcountTeacher']);
        // var_dump($role);
        // var_dump($_SESSION[SESS_USER_HOME]['id']);
        $admin_id = $_SESSION[SESS_USER_HOME]['id'];
        //已处理的反馈意见和结果弹窗提示
        if(in_array(WebRole::Student[XPHEnum::Value],$role) && (in_array(WebRole::StudentInfoOfficer[XPHEnum::Value],$role) || in_array(WebRole::UnitStudentInfoOfficer[XPHEnum::Value],$role))){
            $data['myfeedbackcount']=$this->Student_feedback_model->query('SELECT count(*) as student_num from db_student_feedback where creator = '.$admin_id.' and `status` = '.FeedBackStatus::HaveWrite[XPHEnum::Value].' and is_view = 1')[0]['student_num'];
        }else{
            $data['myfeedbackcount']=$this->Supervisor_feedback_model->query('SELECT count(*) as student_num from db_supervisor_feedback where creator = '.$admin_id.' and `status` = '.FeedBackStatus::HaveWrite[XPHEnum::Value].' and is_view = 1')[0]['student_num'];
        }
        $data['admin_id'] = $admin_id;
        
       
        // var_dump($data);
        exit(retJson('反馈',true,$data));
    }
}
